package com.tensai.study.java.basic.algorithm.recursion;

import org.junit.Test;
import reactor.util.function.*;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 递归
 *
 * @author Tensai
 */
public class DiGuiTest {

	/**
	 * <li>递归变量</li>
	 * <li>归并条件</li>
	 * <li>归并返回值</li>
	 * <li>递归策略</li>
	 * <li>递归公式</li>
	 *
	 * @return Function
	 */
	@SuppressWarnings("unused")
	public <T> Function<Tuple5<T, T, T, Function<T, T>, Function<T, T>>, T> diGui() {
		return Tuple3::getT3;
	}

	/**
	 * 阶乘
	 *
	 * @return 结果
	 */
	public Function<Tuple4<Integer, Integer, Integer, Function<Integer, Integer>>,
			Integer> test1() {
		return tp5 -> {
			if (tp5.getT1().equals(tp5.getT2())) {
				return tp5.getT3();
			} else {
				Tuple4<Integer, Integer, Integer, Function<Integer, Integer>> tpl4 = Tuples.of(
						tp5.getT4().apply(tp5.getT1()), tp5.getT2(), tp5.getT3(), tp5.getT4()
				);
				return tp5.getT4().apply(test1().apply(tpl4));
			}
		};
	}

	@Test
	public void test() {
		@SuppressWarnings("all")
		List<Tuple2<Predicate<Integer>, Integer>> pl = new ArrayList<Tuple2<Predicate<Integer>, Integer>>() {{
			add(Tuples.of(x -> x == 1, 1));
		}};
//		Function<Integer, Integer> f = x->test2()
	}

	public Function<Integer, Integer> test2(Function<Integer, Integer> f, List<Tuple2<Predicate<Integer>, Integer>> pl) {
		return x -> {
			for (Tuple2<Predicate<Integer>, Integer> tp2 : pl) {
				if (tp2.getT1().test(x)) {
					return tp2.getT2();
				}
			}
			return f.apply(x);
		};
	}


}
